package cn.eyecool.qmuidemo.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.Random;

import cn.eyecool.qmuidemo.FragmentActivity;
import cn.eyecool.qmuidemo.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TwoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TwoFragment extends BaseFragment {

    private View mStatusView;
    private QMUITopBar mTopBar;

    private String TAG = FragmentActivity.TAG;
    private String CLASSNAME = getClass().getSimpleName();

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public TwoFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment TwoFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static TwoFragment newInstance(String param1, String param2) {
        TwoFragment fragment = new TwoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, CLASSNAME + " onCreated...");
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i(TAG, CLASSNAME + " onCreateView...");
        // Inflate the layout for this fragment
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mStatusView = findViewById(R.id.fake_status_bar);
        mTopBar = rootView.findViewById(R.id.topbar);
        mTopBar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.app_color_blue));
        mTopBar.addLeftBackImageButton().setOnClickListener(v -> back());
        findViewById(R.id.startThree).setOnClickListener(v -> addFragmentToBackStack(getActivity(), R.id.rootLayout, ThreeFragment.newInstance("3", "3")));
        mTopBar.setTitle(CLASSNAME);

        Random random = new Random();
        int color = 0xff000000 | random.nextInt(0xffffff);
        mTopBar.setBackgroundColor(color);
        mStatusView.setBackgroundColor(color);
        mStatusView.post(() -> {
            Log.i(TAG, "statusBarHeight = " + statusBarHeight);
            ViewGroup.LayoutParams lp = mStatusView.getLayoutParams();
            lp.height = statusBarHeight;
            mStatusView.setLayoutParams(lp);
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_two;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, CLASSNAME + " onResume...");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e(TAG, CLASSNAME + " onPause...");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, CLASSNAME + " onStop...");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.e(TAG, CLASSNAME + " onDestroyView...");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, CLASSNAME + " onDestroy...");
    }
}